﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Surface.Presentation.Controls;
using SurfaceInteraction.MetaData.ServiceContracts;
using SurfaceInteraction.UI.Surface.Helper;
using SurfaceInteraction.UI.Surface.Services;
using System.Windows.Media;
using System.Windows.Threading;

namespace SurfaceInteraction.UI.Surface
{
    /// <summary>
    /// Interaction logic for UniversalByteTagView.xaml
    /// </summary>
    public partial class GeneralTagVisualization : TagVisualization
    {
        private DateTime _lastScreenShot;

        public GeneralTagVisualization()
        {
            InitializeComponent();
            this.Moved += new TagVisualizerEventHandler(UniversalByteTagView_Moved);
            this.LostTag += new RoutedEventHandler(GeneralTagVisualization_LostTag);
            this.GotTag += new RoutedEventHandler(GeneralTagVisualization_GotTag);
            _lastScreenShot = DateTime.Now;

            
        }

        void GeneralTagVisualization_GotTag(object sender, RoutedEventArgs e)
        {
            //HostService.DownloadRequestEvent += new MetaData.DownloadRequestEventHandler(HostService_DownloadRequestEvent);
        }

        //void HostService_DownloadRequestEvent(object sender, MetaData.FileExchangeRequestEventArgs e)
        //{
        //     a download request from the handled device is arrived
        //     now we have to check witch content is under the divice, get the correspondig image and send to the service.

        //    get the scatterview
        //    this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
        //            (System.Action)(() =>
        //            {


        //                DependencyObject parent = VisualTreeHelper.GetParent(this);
        //                while (parent as ScatterView == null)
        //                {
        //                    parent = VisualTreeHelper.GetParent(parent);
        //                }

        //                 get the position of the control without rotation
        //                Point thisA = new Point((this.Center.X - (this.ActualWidth / 2)), (this.Center.Y - (this.ActualHeight / 2)));
        //                Point thisB = new Point((this.Center.X + (this.ActualWidth / 2)), (this.Center.Y - (this.ActualHeight / 2)));
        //                Point thisC = new Point((this.Center.X + (this.ActualWidth / 2)), (this.Center.Y + (this.ActualHeight / 2)));
        //                Point thisD = new Point((this.Center.X - (this.ActualWidth / 2)), (this.Center.Y + (this.ActualHeight / 2)));

        //                 get the position of the rotated control with some math calculation
        //                Point thisA1 = thisA.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
        //                Point thisB1 = thisB.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
        //                Point thisC1 = thisC.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
        //                Point thisD1 = thisD.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);

        //                Rect r1 = new Rect(thisA1, thisC1);

        //                foreach (ScatterViewItem item in ((ScatterView)parent).Items)
        //                {
        //                    if (item.Content is Content)
        //                    {

        //                        check if the item is under 

        //                         get the position of the control without rotation
        //                        Point itemA = new Point((item.Center.X - (item.ActualWidth / 2)), (item.Center.Y - (item.ActualHeight / 2)));
        //                        Point itemB = new Point((item.Center.X + (item.ActualWidth / 2)), (item.Center.Y - (item.ActualHeight / 2)));
        //                        Point itemC = new Point((item.Center.X + (item.ActualWidth / 2)), (item.Center.Y + (item.ActualHeight / 2)));
        //                        Point itemD = new Point((item.Center.X - (item.ActualWidth / 2)), (item.Center.Y + (item.ActualHeight / 2)));

        //                         get the position of the rotated control with some math calculation
        //                        Point itemA1 = itemA.GetRotatedPoint(item.Orientation, item.ActualWidth, item.ActualHeight);
        //                        Point itemB1 = itemB.GetRotatedPoint(item.Orientation, item.ActualWidth, item.ActualHeight);
        //                        Point itemC1 = itemC.GetRotatedPoint(item.Orientation, item.ActualWidth, item.ActualHeight);
        //                        Point itemD1 = itemD.GetRotatedPoint(item.Orientation, item.ActualWidth, item.ActualHeight);


        //                        if (r1.Contains(itemA1) || r1.Contains(itemB1) || r1.Contains(itemC1) || r1.Contains(itemD1))
        //                        {
        //                            got it!
        //                            string uri = (string)((Content)item.Content).DataContext;
        //                            if (string.IsNullOrEmpty(uri) == false)
        //                            {
        //                                HostService.SendImage(e.ImageId, Serializer.FileToByteArray(uri));
        //                            }

        //                             for this sample I evaluate only the first item contained, in a production application I would evaluate the position of the touchdown on the device
        //                             with the position of the items (if there is more than one) under the device..
        //                            break;
        //                        }
        //                    }
        //                }
        //            }));

        //}

        void GeneralTagVisualization_LostTag(object sender, RoutedEventArgs e)
        {
            // device has been removed from surface so the device's screen have to be set to null
            SurfaceFrame sf = new SurfaceFrame();
            sf.Frame = null;
            sf.FrameDate = DateTime.Now;
            sf.Id = (byte)VisualizedTag.Value;
            HostService.SetFrame(sf);

            //HostService.DownloadRequestEvent -= new MetaData.DownloadRequestEventHandler(HostService_DownloadRequestEvent);
        }

        void UniversalByteTagView_Moved(object sender, TagVisualizerEventArgs e)
        {
            if (this._lastScreenShot.AddMilliseconds(Constants.IDLE_TIMEOUT) <= DateTime.Now)
            {
                // get the position of the control without rotation
                Point a = new Point((this.Center.X - (this.ActualWidth / 2)), (this.Center.Y - (this.ActualHeight / 2)));
                Point b = new Point((this.Center.X + (this.ActualWidth / 2)), (this.Center.Y - (this.ActualHeight / 2)));
                Point c = new Point((this.Center.X + (this.ActualWidth / 2)), (this.Center.Y + (this.ActualHeight / 2)));
                Point d = new Point((this.Center.X - (this.ActualWidth / 2)), (this.Center.Y + (this.ActualHeight / 2)));

                // get the position of the rotated control with some math calculation
                Point a1 = a.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
                Point b1 = b.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
                Point c1 = c.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);
                Point d1 = d.GetRotatedPoint(this.Orientation, this.ActualWidth, this.ActualHeight);

                // we can reach the same result using a rotate transform                
                // RotateTransform rotateTransform = new RotateTransform(this.Orientation)
                // {
                //    CenterX = this.Center.X,
                //    CenterY = this.Center.Y
                // };
                // Polygon pol = new Polygon();
                // pol.Points = new PointCollection() { a, b, c, d };                
                // pol.RenderTransform = rotateTransform;
                // Point a11 = pol.RenderTransform.Transform(a);
                // Point b11 = pol.RenderTransform.Transform(b);
                // Point c11 = pol.RenderTransform.Transform(c);
                // Point d11 = pol.RenderTransform.Transform(d);

                _lastScreenShot = DateTime.Now;
                
                SurfaceFrame sf = new SurfaceFrame();
                
                sf.Frame = Application.Current.MainWindow.GetImage(1, 90, new List<Point>() { a1, b1, c1, d1 }, this.Orientation, this.Center);
                sf.FrameDate = _lastScreenShot;
                sf.Id = (byte)VisualizedTag.Value;
                HostService.SetFrame(sf);
            }
        }
    }
}
